﻿using System.Collections.Generic;
using System.Web.Routing;
using Nop.Core.Plugins;
using Nop.Services.Cms;
using Nop.Services.Configuration;
using Nop.Services.Localization;
using Nop.Plugin.Misc.TabManager.Data;
using Nop.Web.Framework.Menu;
using Nop.Plugin.Misc.TabManager.Models;

namespace Nop.Plugin.Misc.TabManager
{
    /// <summary>
    /// Live person provider
    /// </summary>
    public class TabManagerPlugin : BasePlugin, IAdminMenuPlugin, IPlugin
    {
        private readonly TabManagerContext _TabContext;
        private readonly ISettingService _settingService;

        public TabManagerPlugin(TabManagerContext ctx, ISettingService settingService)
        {
            this._TabContext = ctx;
            this._settingService = settingService;
        }


        public bool Authenticate()
        {
            return true;
        }

        public SiteMapNode BuildMenuItem()
        {
            SiteMapNode node2 = new SiteMapNode
            {
                Title = "Tab Manager",
                ControllerName = "TabManager",
                ActionName = "ConfigureTabs",
                Visible = true
            };
            RouteValueDictionary dictionary = new RouteValueDictionary();
            dictionary.Add("area", null);
            node2.RouteValues = dictionary;
            return node2;
        }


        /// <summary>
        /// Install plugin
        /// </summary>
        public override void Install()
        {
            //settings
            var settings = new TabSettings
            {
                CacheMenuItems = false,
                IncludeTopics = false,
                IncludeCategories = false,
                TrackChangeHistory = false,
                UseKendoUIMenu = false,
                MainMenuClass = "top-menu",
                SubMenuContainerClass = "top-menu-triangle",
                SubMenuClass = "sublist firstLevel",
                MenuItemClass = ""
            };
            _settingService.SaveSetting(settings);

            this._TabContext.Install();

            this.AddOrUpdatePluginLocaleResource("TabManager.CacheMenuItems", "Cache Menu Items");
            this.AddOrUpdatePluginLocaleResource("TabManager.IncludeTopics", "Include Topics");
            this.AddOrUpdatePluginLocaleResource("TabManager.IncludeCategories", "Include Categories");
            this.AddOrUpdatePluginLocaleResource("TabManager.TrackChangeHistory", "Track Change History");
            this.AddOrUpdatePluginLocaleResource("TabManager.UseKendoUIMenu", "Use KendoUI Menu");
            this.AddOrUpdatePluginLocaleResource("TabManager.MainMenuClass", "Main Menu Class");
            this.AddOrUpdatePluginLocaleResource("TabManager.SubMenuContainerClass", "Sub Menu Container Class");
            this.AddOrUpdatePluginLocaleResource("TabManager.SubMenuClass", "Sub Menu Class");
            this.AddOrUpdatePluginLocaleResource("TabManager.MenuItemClass", "Menu Item Class");

            base.Install();
        }

        /// <summary>
        /// Uninstall plugin
        /// </summary>
        public override void Uninstall()
        {
            //settings
            var stt = _settingService.GetSettingByKey<TabSettings>("TabManager.TabSettings");
            if (stt != null)
                _settingService.DeleteSetting<TabSettings>();

            this._TabContext.Uninstall();

            //locales
            this.DeletePluginLocaleResource("TabManager.CacheMenuItems");
            this.DeletePluginLocaleResource("TabManager.IncludeTopics");
            this.DeletePluginLocaleResource("TabManager.IncludeCategories");
            this.DeletePluginLocaleResource("TabManager.TrackChangeHistory");
            this.DeletePluginLocaleResource("TabManager.UseKendoUIMenu");
            this.DeletePluginLocaleResource("TabManager.MainMenuClass");
            this.DeletePluginLocaleResource("TabManager.SubMenuContainerClass");
            this.DeletePluginLocaleResource("TabManager.SubMenuClass");
            this.DeletePluginLocaleResource("TabManager.MenuItemClass");

            base.Uninstall();
        }
    }
}
